<template>
<main class="home-view">
  <div class="header" ref="refHeader">
    <HeaderComponent />
  </div>
  <div class="left" ref="refLeft">
    <div class="left-one-wrapper">
      <TodayWarning />
    </div>
    <div class="left-two-wrapper">
      <RepositoryInfo />
    </div>
  </div>
  <div class="center" ref="refCenter">
  </div>
  <div class="right" ref="refRight">
    <div class="right-one-wrapper">
      <HistoryWarning />
    </div>
    <div class="right-two-wrapper">
      <PersonnelLocation />
    </div>
  </div>
  <div class="footer"></div>
</main>
</template>

<script setup>
// const targetRoute = { name: 'EmergencyPage' }
const pageDuration = 400

// import CenterComponent from './center/CenterComponent.vue'
import HeaderComponent from './header/index.vue'
import { onBeforeRouteLeave, useRouter, useRoute } from 'vue-router'
import { onMounted, ref } from 'vue'
import PersonnelLocation from './left-right/PersonnelLocation/index.vue'
import RepositoryInfo from './left-right/RepositoryInfo/index.vue'
import TodayWarning from './left-right/TodayWarning/index.vue'
import HistoryWarning from './left-right/HistoryWarning/index.vue'

// import leaveSvg from '@/assets/icons/header/leave.svg?raw'

// const router = useRouter()
// const toRoute = (route) => {
//   router.push(route)
// }

const refHeader = ref(null)
const refLeft = ref(null)
const refRight = ref(null)
const refCenter = ref(null)

onMounted(async () => {
  setTimeout(() => {
    toggleAnimation(true)
  }, pageDuration)
})

onBeforeRouteLeave(async (to, from, next) => {
  await toggleAnimation(false)
  next()
})

async function toggleAnimation(visible) {
  await Promise.all([toggleHeader(visible), toggleLeft(visible), toggleRight(visible), toggleCenter(visible)])

  function toggleHeader(visible) {
    return new Promise((resolve) => {
      const headerAnimation = new Animation(
        new KeyframeEffect(
          refHeader.value,
          [{ transform: 'translateY(-100%)' }, { transform: 'translateY(0)' }],
          { duration: pageDuration, fill: 'both' },
        ),
      )
      headerAnimation.playbackRate = visible ? 1 : -1
      headerAnimation.play()
      headerAnimation.onfinish = () => {
        headerAnimation.commitStyles()
        resolve()
      }
    })
  }

  function toggleLeft(visible) {
    return new Promise((resolve) => {
      const leftAnimation = new Animation(
        new KeyframeEffect(
          refLeft.value,
          [{ transform: 'translateX(-100%)' }, { transform: 'translateX(0)' }],
          { duration: pageDuration, fill: 'both' },
        ),
      )
      leftAnimation.playbackRate = visible ? 1 : -1
      leftAnimation.play()
      leftAnimation.onfinish = () => {
        leftAnimation.commitStyles()
        resolve()
      }
    })
  }

  function toggleRight(visible) {
    return new Promise((resolve) => {
      const rightAnimation = new Animation(
        new KeyframeEffect(
          refRight.value,
          [{ transform: 'translateX(100%)' }, { transform: 'translateX(0)' }],
          { duration: pageDuration, fill: 'both' },
        ),
      )
      rightAnimation.playbackRate = visible ? 1 : -1
      rightAnimation.play()
      rightAnimation.onfinish = () => {
        rightAnimation.commitStyles()
        resolve()
      }
    })
  }

  // todo 这里实现得不好，opacity 影响到两边栏的显隐，Dom结构的关系
  // todo 考虑将 three 和cesium 放在center， center 平铺整个grid的下部
  function toggleCenter(visible) {
    return new Promise((resolve) => {
      const center = document.querySelector('.root-3d')
      const centerAnimation = new Animation(
        new KeyframeEffect(
          center,
          [
            { opacity: 0 },
            { opacity: 1 },
          ],
          { duration: pageDuration, fill: 'both' },
        ),
      )
      centerAnimation.playbackRate = visible ? 1 : -1
      centerAnimation.play()
      centerAnimation.onfinish = () => {
        centerAnimation.commitStyles()
        resolve()
      }
    })
  }
}
</script>

<style lang="scss" scoped>
@use 'sass:math';

.home-view {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  $padding-left-right: 10px;
  display: grid;
  grid-template-columns: 1fr 960px 1fr;
  grid-template-rows: 50px 1fr 10px;
  grid-template-areas:
    'head head head'
    'left center right'
    'footer footer footer';

  > :nth-child(n) {
    user-select: none;
    // border: 1px solid #fff;
  }

  .header {
    transform: translateY(-100%);
    grid-area: head;
  }

  $left-right-padding-block: 10px;

  .left {
    pointer-events: auto;
    padding-left: $padding-left-right;
    // padding-top: $left-right-padding-block;
    display: grid;
    grid-template-columns: 100%;
    grid-template-rows: 1fr 1fr;
    gap: 20px;
    transform: translateX(-100%);
    overflow: hidden;


    > :nth-child(1) {
      min-height: 0;
    }

    > :nth-child(2) {
      min-height: 0px;
    }
  }

  .right {
    pointer-events: auto;
    padding-right: $padding-left-right;
    // padding-top: $left-right-padding-block;
    display: grid;
    gap: 20px;
    grid-template-rows: 1fr 1fr;
    grid-template-columns: 100%;
    transform: translateX(100%);

    > :nth-child(1) {
      min-height: 0;
    }

    > :nth-child(2) {
      min-height: 0px;
    }

    overflow: hidden;
  }

  .center {
    grid-area: center;
    display: grid;
  }

  .footer {
    grid-area: footer;
  }
}
</style>
